## Brooks and Kurtz 2012 IO

library(foreign) 
library(Amelia)

## Load original dataset
bk2012 <- read.dta("BK2012 IO Rep Data.dta")
head(bk2012)
dim(bk2012)

## Drop ID vars
bk2012$country <- NULL

## How many variables? 32: no reduction necessary
dim(bk2012)

## Imputation
## What is average percentage of missing data?
NAs <- function(x) {
    as.vector(apply(x, 2, function(x) length(which(is.na(x)))))
    }
NAs(bk2012)
mean(NAs(bk2012)/nrow(bk2012))*100

## Thus: 5 imputations

set.seed(02138)
bk2012.out <- amelia(bk2012, m = 5, ts = "year", cs = "conum", polytime = 3, lags = c("kaopen", "isi_objective", "diffusionisipeer_wtavg", "diffusionisipeer_wtavg_partisan", "diffusionisipeer_wtavg_checks", "diffusionisipeer_wtavg_imf", "diffusionisipeer_wtavg_usffr"), empri = 0.01*nrow(bk2012))

write.amelia(obj=bk2012.out, file.stem = "BK2012 IO Imp Data", format = "dta", separate = FALSE)